/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.sessionbeans;

import it.polimi.meteocal.entities.Event;
import it.polimi.meteocal.entities.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class EventManager {

    @PersistenceContext
    EntityManager em;
    
    @Inject
    Principal principal;
    
    public List<Event> loadOrganizedEvents(User organizer){ 
        List<Event> organizedEvents = new ArrayList<>();
        for(Event e : organizer.getEventList()){
            if(e.getValid() == true){
                organizedEvents.add(e);
            }
        }            
        return organizedEvents;
    }
    
    public List<Event> loadParticipantEvents(User participant){
        List<Event> participantEvents = new ArrayList<>();
        for(Event e : participant.getParticipantEventList()){
            if(e.getValid() == true){
                participantEvents.add(e);
            }
        }           
        return participantEvents;
    }
    
    public Event loadEventByIde(Event e){
        return (Event) em.createNamedQuery("Event.findByIde").setParameter("ide", e.getIde()).getSingleResult();
    }
    
    public void saveNewEvent(Event e){
        em.persist(e);
    }
    
    public void saveModifiedEvent(Event e){
        em.merge(e);
    }
    
    public void setNonValidEvent(Event e){
        e.setValid(false);
        em.merge(e);
    }

}
